package top.magicpotato.fast.quartz.connection.provider;

import org.quartz.utils.ConnectionProvider;
import top.magicpotato.fast.quartz.utils.SpringContextUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author ycl
 * 标准实现，从spring容器中获取
 */
public class StdConnectionProvider implements ConnectionProvider {

    private static DataSource dataSource;

    @Override
    public Connection getConnection() throws SQLException {
        if (dataSource == null) {
            dataSource = SpringContextUtil.getBean(DataSource.class);
        }
        return dataSource.getConnection();
    }

    @Override
    public void shutdown() throws SQLException {
    }

    @Override
    public void initialize() throws SQLException {
    }

    public static void setDataSource(DataSource dataSource) {
        StdConnectionProvider.dataSource = dataSource;
    }
}
